package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"yklx1/cache"
	"yklx1/models"
	"yklx1/pkg"
)

type WWW struct {
	beego.Controller
}
type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (w *WWW) Login() {
	usersname := w.GetString("usersname")
	password := w.GetString("password")
	if usersname == "" || password == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "字段不能为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	redsikey := "redis_id" + usersname
	errCount, _ := cache.Rdb.Get(cache.Ctx, redsikey).Int()
	if errCount >= 3 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误三次禁止登录",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var err error
	var user models.Users
	err = user.FinData(usersname, &user)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户不能存在",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	if user.Password != pkg.Md5str(password) {
		cache.Rdb.Incr(cache.Ctx, redsikey)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redsikey, 3, time.Hour*1)
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "密码错误3次,禁止登录",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误",
			Data: nil,
		}
		w.ServeJSON()
		return

	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "登录成功",
		Data: user,
	}
	w.ServeJSON()
}
func (w *WWW) RolenodegxbiaoAdd() {
	rid, _ := w.GetInt("rid", 0)
	nid, _ := w.GetInt("nid", 0)
	if rid == 0 || nid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色ID或者权限ID不能为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	data := models.Rolenodegxbiao{
		Rid: rid,
		Nid: nid,
	}
	_, err := data.AddData(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()

}

func (w *WWW) UserRole() {
	uid, _ := w.GetInt("uid", 0)
	if uid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户uid为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var Role models.Role
	list, err := Role.List(uid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
}
func (w *WWW) NodeRole() {
	rid, _ := w.GetInt("rid", 0)
	if rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色rid为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var node models.Node
	lists, err := node.Lists(rid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: lists,
	}
	w.ServeJSON()
}

// 物流添加数据
func (w *WWW) CompanyAdd() {
	companyName := w.GetString("companyName")
	if companyName == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流名称不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	data := models.Company{
		CompanyName: companyName,
	}
	_, err := data.AddDatas(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 0,
		Msg:  "物流添加成功",
		Data: data,
	}
	w.ServeJSON()
}

func (w *WWW) OrdersAdd() {
	orders := w.GetString("orders")
	cid, _ := w.GetInt("cid")
	eid, _ := w.GetInt("eid")
	if orders == "" || cid == 0 || eid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流名称不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	data := models.Orders{
		Orders: orders,
		Cid:    cid,
		Eid:    eid,
	}
	_, err := data.OrdersData(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "单号添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "单号添加成功",
		Data: data,
	}
	w.ServeJSON()
}

func (w *WWW) OrderList() {
	page, _ := w.GetInt("page")
	size, _ := w.GetInt("size")
	var Listss []models.Orders
	var err error
	listKey := "Listid" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, listKey).Result()
	if cacheData == "" {
		var orders models.Orders
		Listss, err = orders.PageList(page, size)
		if err != nil {
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "数据查询失败",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		cacheJson, _ := json.Marshal(Listss)
		cache.Rdb.Set(cache.Ctx, listKey, cacheJson, time.Hour*1)
	} else {
		err = json.Unmarshal([]byte(cacheData), &Listss)
		if err != nil {
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "数据查询失败",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
	}
	var count int64
	models.DB.Table("users").Count(&count)
	pageData := make(map[string]interface{})
	pageData["count"] = count
	pageData["Listss"] = Listss
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: pageData,
	}
	w.ServeJSON()
}

// 32.快递详情 (32,33)
func (w *WWW) OrderDetail() {
	id, _ := w.GetInt("id")
	if id == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "id不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var orders models.Orders
	var err error

	key := "redisid" + strconv.Itoa(id)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		if orders, err = orders.OrderDetail(id); err != nil {
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "数据查询失败",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		jsonData, _ := json.Marshal(orders)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*2)
	} else {
		if err = json.Unmarshal([]byte(cacheData), &orders); err != nil {
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "数据查询失败",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: orders,
	}
	w.ServeJSON()
}
